package com.wing.order.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.OrderDeliverySign;
import com.wing.order.model.request.OrderDeliverySignForm;
import com.wing.order.model.response.OrderDeliverySignDetailVO;
import com.wing.order.model.response.OrderDeliverySignVO;
import com.wing.order.service.OrderDeliverySignService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-签收记录管理", tags = "#/order/orderDeliverySign")
@RestController("orderDeliverySignController")
@RequestMapping("order/admin/orderDeliverySign")
@RequiredArgsConstructor
public class OrderDeliverySignController {

    private final OrderDeliverySignService orderDeliverySignService;


    @ApiOperation(value = "查询所有签收记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "签收记录编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderDeliverySignVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,OrderDeliverySignVO orderDeliverySignVO
    ){

        IPage<OrderDeliverySignVO> list = orderDeliverySignService.getOrderDeliverySignVOPageList(page,limit,orderDeliverySignVO);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询签收记录详情", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "signId", value = "签收记录id", defaultValue = "", example = "", required = true,dataType = "Long", paramType = "query")
    })
    @GetMapping("/getSignDetailList")
    public PageResult<OrderDeliverySignDetailVO> getSignDetailList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long signId
    ){

        IPage<OrderDeliverySignDetailVO> list = orderDeliverySignService.getSignDetailList(page,limit,signId);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加签收记录", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(OrderDeliverySignForm.Add.class) OrderDeliverySignForm orderDeliverySignForm) {
        if (orderDeliverySignService.add(orderDeliverySignForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改签收记录", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(OrderDeliverySignForm.Update.class) OrderDeliverySignForm orderDeliverySignForm) {
        OrderDeliverySign orderDeliverySign = orderDeliverySignService.getById(orderDeliverySignForm.getId());
        if(orderDeliverySign == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderDeliverySign.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderDeliverySignForm, orderDeliverySign, copyOptions);
        if (orderDeliverySignService.updateById(orderDeliverySign)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除签收记录", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderDeliverySignId", value = "签收记录id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long orderDeliverySignId) {
        if (orderDeliverySignService.removeById(orderDeliverySignId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
